import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import { ElMessage } from 'element-plus';
import { staticRoutes } from './routes';

const router = createRouter({
  history: createWebHashHistory(),
  routes: staticRoutes,
});

router.beforeEach((to, from, next) => {
  //刷新后动态路由丢失,找不到页面时,就跳转layout页面重新进行初始化
  if (to.matched.length == 0) return next('/layout'); 
  
  // if (to.path === '/login') localStorage.clear();
  if (to.meta.title) document.title = ('智慧选课 - ' + to.meta.title) as string;

  //如果跳转的是login，那么本地缓存清空
  if (to.path == '/login') localStorage.clear();

  next(); //其他情况就直接放过
});

//后置守卫
router.afterEach((to, from) => {
  if (from.path === '/login') ElMessage.success('登陆成功');
});

export default router;
